package com.extension.company.view.home;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.extension.company.admin.R;
import com.extension.company.model.entity.LeftListBean;
import com.extension.company.model.entity.RightListBean;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.widget.LinearSpaceItemDecoration;
import com.extension.company.contract.main.MakeMoneyContract;
import com.extension.company.model.entity.MakeMoneyData;
import com.extension.company.presenter.home.MakeMoneyPrester;
import com.extension.company.view.adapter.MakeMoneyLeftAdapter;
import com.extension.company.view.adapter.MakeMoneyRightAdapter;
import com.extension.company.view.makemoney.SellerPage;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * create zwk
 * data   2019/2/19
 * 描述：fragmentHome中“推荐赚钱”模块
 */
public class MakeMoneyActivity extends BaseTitleActivity<MakeMoneyContract.View, MakeMoneyPrester> implements MakeMoneyContract.View,
		AdapterView.OnItemClickListener,
		MakeMoneyRightAdapter.rightClassfy,
		OnRefreshListener, OnLoadMoreListener {

	@BindView(R.id.mListView_Left)
	ListView mListView_Left;
	@BindView(R.id.mListView_Right)
	RecyclerView mListView_Right;
	@BindView(R.id.right_smart)
	SmartRefreshLayout smartRefreshLayout;

	private final static int MAX_COUNT = 20; // 一页数据条数
	private final static int START_PAGE = 0;
	private int lastPositionId; // 上次请求的left的id
	private int count = START_PAGE; // 请求的页码数


	private int scrollViewMiddle = 0;
	private int scrollViewWidth = 0;

	private List<MakeMoneyData> list;
	private List<LeftListBean.DataBean.CategoryBean> leftList = new ArrayList<>();
	private List<RightListBean.DataBean.GoodsBean> rightList = new ArrayList<>();
	private MakeMoneyLeftAdapter leftAdapter;
	private MakeMoneyRightAdapter rightAdapter;


	public static void startMakeMoney(Activity activity) {
		Intent intent = new Intent(activity, MakeMoneyActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("全部商品");
		initSmart();
		presenter.requestLeftData(); // 请求左侧的数据

		leftAdapter = new MakeMoneyLeftAdapter(this, leftList);  // 左侧的分类
		mListView_Left.setAdapter(leftAdapter);

		mListView_Right.addItemDecoration(new LinearSpaceItemDecoration(true, 20));
		rightAdapter = new MakeMoneyRightAdapter(this, rightList);  // 右侧的详细的列表
		mListView_Right.setAdapter(rightAdapter);
	}

	/**
	 * 初始化smartRefresh控件
	 */
	private void initSmart() {
		smartRefreshLayout.setEnableRefresh(false);// 关闭刷新功能
		smartRefreshLayout.setEnableLoadMore(false);// 关闭加载更多功能
		smartRefreshLayout.setOnRefreshListener(this);
		smartRefreshLayout.setOnLoadMoreListener(this);
	}

	@Override
	protected void initListener() {
		mListView_Left.setOnItemClickListener(this);
		rightAdapter.setOnRightItemClick(this);
	}

	@Override
	public int getMainLayout() {
		return R.layout.home_makemoney;
	}

	@Override
	public MakeMoneyContract.View createView() {
		return this;
	}

	@Override
	public MakeMoneyPrester createPresenter() {
		return new MakeMoneyPrester();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void leftData(List<LeftListBean.DataBean.CategoryBean> list) {  // 左面的商品的分类列表
		if (null != leftList && leftList.size() > 0) {
			leftList.clear();
		}
		if (null != leftList)
			leftList.addAll(list);
		leftAdapter.notifyDataSetChanged();

		presenter.requestRightData(list.get(0).getId(), count); // 左一右一页
	}

	@Override
	public void rightData(List<RightListBean.DataBean.GoodsBean> list, int leftId) {  // 右面的商品的数据

		if (lastPositionId == leftId) { // 这请求的id 和上次请求的id 相同： 1.加载更多 2  充新点击左item
			if (0 == count) { // 页码是0页 ，充新点击的左item
				rightList.clear();
				rightList.addAll(list);
			} else if (0 < count) { // 在载更多
				rightList.addAll(list);
				if (smartRefreshLayout.getState() == RefreshState.Loading) {
					smartRefreshLayout.finishLoadMore(true);
				}
			}
		} else {
			rightList.clear();
			rightList.addAll(list);
		}
		if (null != list && list.size() == MAX_COUNT) {   // 如果请求一页数据小于20条 ,则说明没有数据了，加载更多无法上滑
			smartRefreshLayout.setEnableLoadMore(true);
		}

		rightAdapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		changeLeftPosition(position);
		leftAdapter.setIndex(position);
		leftAdapter.notifyDataSetChanged();
		mListView_Left.smoothScrollToPositionFromTop(position, mListView_Left.getHeight() / 2 - view.getHeight() / 2);
		LeftListBean.DataBean.CategoryBean bean = (LeftListBean.DataBean.CategoryBean) parent.getAdapter().getItem(position);

		count = START_PAGE; // 重置请求的其实页码
		presenter.requestRightData(bean.getId(), count);  // 左id,右数据
		lastPositionId = bean.getId(); // 记录点击的分类的id值
		smartRefreshLayout.setEnableLoadMore(false); // 切换类别，关闭上拉加载更多功能


	}

	@Override
	public void righClick(int position) { // 右边list的点击事件

		SellerPage.startActivity(MakeMoneyActivity.this);

	}


	private void changeLeftPosition(int position) {
		int x = 0;
		if (position <= ((mListView_Left.getLastVisiblePosition() + mListView_Left.getFirstVisiblePosition()) / 2)) {
			x = position - getScrollViewMiddle();
		} else {
			x = position + getScrollViewMiddle();
		}
		mListView_Left.smoothScrollToPosition(x);
	}

	private int getScrollViewMiddle() {
		scrollViewMiddle = getScrollViewHeight() / 2;
		return scrollViewMiddle;
	}

	private int getScrollViewHeight() {
		scrollViewWidth = mListView_Left.getLastVisiblePosition();
		Log.d("zwk", "最后 " + scrollViewWidth);
		Log.d("zwk", (mListView_Left.getLastVisiblePosition() - mListView_Left.getFirstVisiblePosition()) + "一屏 多少个");
		return scrollViewWidth;
	}

	@Override
	public void onLoadMore(@NonNull RefreshLayout refreshLayout) { // 刷新
		++count;
		presenter.requestRightData(lastPositionId, count); // 根据页码数请求更多的数据
	}

	@Override
	public void onRefresh(@NonNull RefreshLayout refreshLayout) { // 刷新
	}
}
